package com.yiren.dbaa.module.exam.view.id;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.drawerlayout.widget.DrawerLayout;

import com.bumptech.glide.Glide;
import com.yiren.dbaa.R;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.common.view.dialog.RiskControlDialog;
import com.yiren.dbaa.module.exam.contract.ExamStudentContract;
import com.yiren.dbaa.module.exam.presenter.ExamStudentPresenter;
import com.yiren.dbaa.module.exam.view.adapter.StudentExamAdapter;
import com.yiren.dbaa.net.bean.StudentBean;
import com.yiren.dbaa.util.NetWorkUtil;
import com.yiren.dbaa.view.dialog.QrCodeImageDialog;
import com.yiren.dbaa.zxing.encoding.EncodingUtils;
import com.yiren.dbaa.zxing.encoding.ZXingUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WANGRONGXIANG
 * on 2022/5/5
 * Describe 考试页面通用学员信息相关id
 */
@SuppressLint("WrongConstant")
public class ExamStudentId implements ExamStudentContract.View, StudentExamAdapter.OnStudentClickListener {

    private Activity mActivity;
    private QrCodeImageDialog qrCodeImageDialog;   //显示二维码的弹框

    private DrawerLayout mDrawerLayout;
    private LinearLayout llStuSelect;    //学员选择页面
    private ListView lvStudent;          //其它学员列表
    private RelativeLayout rlQrCode;     //二维码布局
    private ImageView ivQrCode;          //二维码
    private LinearLayout llStuComplete;  //学员选择完成布局
    private TextView tvSelectStu;        //选择的学员姓名

    private ImageView ivStuHead;         //界面上显示的学员头像
    private TextView tvStuName;          //界面上显示的学员姓名

    private ExamStudentContract.Presenter studentPresenter;

    private String qrCodeUrl;            //二维码链接
    protected StudentBean studentBean;   //当前的学员信息
    protected List<StudentBean> studentInfos = new ArrayList<>(); //其它学员信息
    private StudentExamAdapter studentExamAdapter;//其它学员列表适配器

    public void setStudentBean(StudentBean studentBean) {
        this.studentBean = studentBean;
    }

    public StudentBean getStudentBean() {
        return studentBean;
    }

    public ExamStudentId(Activity mActivity) {
        this.mActivity = mActivity;
        studentPresenter = new ExamStudentPresenter(mActivity);
        initView();
        setAdapter();
        setListener();
    }

    private void initView() {
        mDrawerLayout = mActivity.findViewById(R.id.drawer_layout);
        llStuSelect = mActivity.findViewById(R.id.ll_stu_select);
        lvStudent = mActivity.findViewById(R.id.lv_student);
        rlQrCode = mActivity.findViewById(R.id.rl_qr_code);
        ivQrCode = mActivity.findViewById(R.id.iv_qr_code);
        llStuComplete = mActivity.findViewById(R.id.ll_stu_complete);
        tvSelectStu = mActivity.findViewById(R.id.tv_select_stu);
        ivStuHead = mActivity.findViewById(R.id.iv_stu_head);
        tvStuName = mActivity.findViewById(R.id.tv_stu_name);
    }

    /**
     * 设置子考试页面公用的适配器
     */
    private void setAdapter() {
        studentExamAdapter = new StudentExamAdapter(mActivity, studentInfos, this);
        lvStudent.setAdapter(studentExamAdapter);
    }

    private void setListener() {
        //选择学员返回
        mActivity.findViewById(R.id.iv_stu_return).setOnClickListener(v -> mDrawerLayout.closeDrawer(Gravity.START));
        //二维码放大
        ivQrCode.setOnClickListener(v -> showBigQrCode());
        //关闭侧拉框的事件
        mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerClosed(View drawerView) {
                studentPresenter.removeCallBack();
            }
        });
    }

    @Override
    public void onStudentClick(int position) {
        studentPresenter.filterStuExam(studentInfos.get(position), this);
    }

    /**
     * 显示放大的练车二维码
     */
    private void showBigQrCode() {
        ivQrCode.setDrawingCacheEnabled(true);
        Bitmap bitmap = EncodingUtils.createQRImage(qrCodeUrl, 200, 200);
        qrCodeImageDialog = new QrCodeImageDialog(mActivity, 0, 0, bitmap);
        qrCodeImageDialog.show();
    }

    /**
     * 打开学员选择侧侧拉框
     */
    public void openStudentDrawer() {
        llStuSelect.setVisibility(View.VISIBLE);
        llStuComplete.setVisibility(View.GONE);
        mDrawerLayout.openDrawer(Gravity.START);
        lvStudent.setSelection(0);
    }

    /**
     * 设置页面上学员信息
     */
    public void setStudentInfo() {
        if (null != studentBean) {
            Glide.with(mActivity).load(AddressConstant.getStuPicUrl(studentBean.getStudent())).placeholder(R.mipmap.default_icon).into(ivStuHead);
            tvStuName.setText(mActivity.getString(R.string.wegi_tv_name_nj) + studentBean.getStudentName());
        }
    }

    /**
     * 侧拉框选择学员完成
     */
    public void selectStuComplete() {
        llStuSelect.setVisibility(View.GONE);
        llStuComplete.setVisibility(View.VISIBLE);
        tvSelectStu.setText(studentBean.getStudentName());
        if (null != mDrawerLayout) {
            new Handler().postDelayed(() -> mDrawerLayout.closeDrawer(Gravity.START), 2000);
        }
    }

    /**
     * 获取快捷练车二维码链接
     */
    public void getQrCode(String model) {
        if (NetWorkUtil.isNetworkAvailable(mActivity)) {
            rlQrCode.setVisibility(View.VISIBLE);
            studentPresenter.getStuQrCode(model, studentBean, this);
        } else {
            rlQrCode.setVisibility(View.GONE);
        }
    }

    /**
     * 设置页面上练车学员字体颜色
     */
    public void setTvStuNameColor(int mode) {
        tvStuName.setTextColor(mActivity.getResources().getColor(mode == 1 ? R.color.exam_txt : R.color.white));
    }

    @Override
    public void setStudentQrCode(String qrCodeUrl) {
        this.qrCodeUrl = qrCodeUrl;
        Bitmap qrImage = ZXingUtils.createQRImage(qrCodeUrl, ivQrCode.getHeight(), ivQrCode.getHeight());
        Glide.with(mActivity).load(qrImage).into(ivQrCode);
    }

    @Override
    public void setStudentList(List<StudentBean> students) {
        studentInfos.clear();
        if (null != students) {
            studentInfos.addAll(students);
        }
        studentExamAdapter.notifyDataSetChanged();
    }

    @Override
    public void sweepCodeSuccess(StudentBean student) {
        if (qrCodeImageDialog != null) {
            qrCodeImageDialog.dismiss();
        }
        studentPresenter.filterStuExam(student, this);
    }

    @Override
    public void showRiskControlDialog(int type, String reason, String url) {
        RiskControlDialog.showDeviceUnuseDialog(mActivity, type, reason, url, v -> {
            studentPresenter.getDeviceRiskResult();
        });
    }

    @Override
    public void showGmks(String title, String message, String btName) {
        CommonDialog.showOneBtn(mActivity, title, message, btName, (dialog, which) -> dialog.dismiss());
    }

    @Override
    public void showTimeLess(StudentBean student, String title, String message, String btName1, String btName2) {
        CommonDialog.showTwoBtn(mActivity, title, message, btName1, btName2, (dialog, which) -> {
            if (which == -2) {
                selectStuSuccess(student);
            } else {
                showBigQrCode();
            }
            dialog.dismiss();
        });
    }

    @Override
    public void selectStuSuccess(StudentBean student) {
        studentBean = student;
        setStudentInfo();
        selectStuComplete();
    }
}
